package com.bocai.kelala.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.bocai.kelala.R;
import com.bocai.kelala.bean.MyAddressBean;
import com.bocai.kelala.ui.travel.main.ada.CarNumDialogAdapter;
import com.bocai.kelala.ui.travel.main.ada.DetailAddressAda;

import java.util.List;

/**
 * Created by yuanfei on 2017/8/9.
 */

public class DetailAddressDialog {

    private Context context;
    private Dialog dialog;
    private Display display;
    private ListView listView;
    private LinearLayout lLayout_bg;
    private DetailAddressAda adapter;

    public DetailAddressDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }
    public DetailAddressDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(
                R.layout.view_car_num_dialog, null);

        // 获取自定义Dialog布局中的控件
        lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg);
        listView = (ListView) view.findViewById(R.id.list);

        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);

        // 调整dialog背景大小
        lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.85), LinearLayout.LayoutParams.WRAP_CONTENT));

        return this;
    }

    public DetailAddressDialog setList(List<MyAddressBean> list, final AdapterView.OnItemClickListener onItemClickListener){
        adapter = new DetailAddressAda(context,list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setIndex(position);
                onItemClickListener.onItemClick(parent,view,position,id);
            }
        });
        return this;
    }
    public void show() {
        if (dialog!=null) {
            dialog.show();
        }
    }
    public void dismiss(){
        if (dialog!=null) {
            dialog.dismiss();
        }
    }

}
